package com.hengxinguotong.hxgtwygsp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * TODO RecyclerView分隔线
 *
 * @author hjz
 * @version V1.0.0
 * @since 2018/8/22 10:13
 */
public class RecycleViewDivider extends RecyclerView.ItemDecoration
{
    private static final int[] ATTRS = new int[]{ android.R.attr.listDivider };
    private final Rect mBounds = new Rect();

    private Context context;

    //画笔
    private Paint mPaint;
    private Drawable mDivider;
    //分割线高度，默认为1px
    private int mDividerHeight = 1;
    //分隔线方向
    private int mOrientation = LinearLayoutManager.HORIZONTAL;

    /**
     * 创建默认分隔线，透明，1px
     *
     * @param context
     * @author hjz
     */
    public RecycleViewDivider(Context context)
    {
        this.context = context;
//        final TypedArray a = context.obtainStyledAttributes(ATTRS);
//        mDivider = a.getDrawable(0);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.TRANSPARENT);
        mPaint.setStyle(Paint.Style.FILL);
    }

    /**
     * 设置分隔线高度，单位dp
     *
     * @param height
     * @return com.hengxinguotong.hxgtproprietor.view.RecycleViewDivider
     * @author hjz
     * @since 2018/8/22 10:12
     */
    public RecycleViewDivider height(int height)
    {
        this.mDividerHeight = height;
        return this;
    }

    /**
     * 设置分隔线方向
     *
     * @param orientation
     * @return com.hengxinguotong.hxgtproprietor.view.RecycleViewDivider
     * @author hjz
     * @since 2018/8/22 10:12
     */
    public RecycleViewDivider orientation(int orientation)
    {
        this.mOrientation = orientation;
        return this;
    }

    /**
     * 设置分隔线颜色
     *
     * @param color
     * @return com.hengxinguotong.hxgtproprietor.view.RecycleViewDivider
     * @author hjz
     * @since 2018/8/22 10:12
     */
    public RecycleViewDivider color(int color)
    {
        mPaint.setColor(ContextCompat.getColor(context, color));
        return this;
    }

    /**
     * 设置图片分隔线
     *
     * @param image
     * @return com.hengxinguotong.hxgtproprietor.view.RecycleViewDivider
     * @author hjz
     * @since 2018/8/22 10:11
     */
    public RecycleViewDivider image(int image)
    {
        mDivider = ContextCompat.getDrawable(context, image);
        mDividerHeight = mDivider.getIntrinsicHeight();
        return this;
    }

    public int dp2px(Context context, float dipValue)
    {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    //获取分割线尺寸
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
    {
        super.getItemOffsets(outRect, view, parent, state);
        outRect.set(0, 0, 0, mDividerHeight);
    }

    //绘制分割线
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state)
    {
        super.onDraw(c, parent, state);
        if (mOrientation == LinearLayoutManager.VERTICAL)
        {
            drawVertical(c, parent);
        } else
        {
            drawHorizontal(c, parent);
        }
    }

    //绘制横向 item 分割线
    private void drawHorizontal(Canvas canvas, RecyclerView parent)
    {
        final int left = parent.getPaddingLeft();
        final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++)
        {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + layoutParams.bottomMargin;
            final int bottom = top + mDividerHeight;
            if (mDivider != null)
            {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null)
            {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    //绘制纵向 item 分割线
    private void drawVertical(Canvas canvas, RecyclerView parent)
    {
        canvas.save();
        final int top;
        final int bottom;

        if (parent.getClipToPadding()) {
            top = parent.getPaddingTop();
            bottom = parent.getHeight() - parent.getPaddingBottom();
            canvas.clipRect(parent.getPaddingLeft(), top,
                    parent.getWidth() - parent.getPaddingRight(), bottom);
        } else {
            top = 0;
            bottom = parent.getHeight();
        }

        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++)
        {
            final View child = parent.getChildAt(i);
            parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
            final int right = mBounds.right + Math.round(child.getTranslationX());
            final int left = right - mDividerHeight;
            if(mDivider != null){
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(canvas);
            }
            if (mPaint != null)
            {
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
        canvas.restore();
    }
}
